#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {

        if (head == nullptr || head->next == nullptr || head->next->next == nullptr) return;

        ListNode* fast = head, * slow = head;
        while (fast && fast->next)
        {
            fast = fast->next->next;
            slow = slow->next;
        }
        ListNode* newhead1 = new ListNode(0);
        ListNode* cur = slow->next;
        slow->next = nullptr;
        while (cur)
        {
            ListNode* next = cur->next;
            cur->next = newhead1->next;
            newhead1->next = cur;
            cur = next;
        }

        ListNode* cur1 = head;
        ListNode* cur2 = newhead1->next;

        ListNode* ret = new ListNode(0);
        ListNode* prev = ret;
        while (cur1)
        {
            prev->next = cur1;
            cur1 = cur1->next;
            prev = prev->next;
            if (cur2)
            {
                prev->next = cur2;
                cur2 = cur2->next;
                prev = prev->next;
            }
        }
        delete newhead1;
        delete ret;
    }
};